Loading our Model

In [1]:
import cv2
from darkflow.net.build import TFNet
import matplotlib.pyplot as plt

%config InlineBackend.figure_format = 'svg'
In [50]:
options = {
    'model' : 'cfg/tiny-yolo-voc-2c.cfg',
    'load' : 1500,
    'threshold' : 0.1,
    'gpu' : 1.0
}


tfnet = TFNet(options)
Parsing cfg/tiny-yolo-voc-2c.cfg
Loading None ...
Finished in 0.0s

Building net ...
Source | Train? | Layer description                | Output size
-------+--------+----------------------------------+---------------
       |        | input                            | (?, 416, 416, 3)
 Init  |  Yep!  | conv 3x3p1_1  +bnorm  leaky      | (?, 416, 416, 16)
 Load  |  Yep!  | maxp 2x2p0_2                     | (?, 208, 208, 16)
 Init  |  Yep!  | conv 3x3p1_1  +bnorm  leaky      | (?, 208, 208, 32)
 Load  |  Yep!  | maxp 2x2p0_2                     | (?, 104, 104, 32)
 Init  |  Yep!  | conv 3x3p1_1  +bnorm  leaky      | (?, 104, 104, 64)
 Load  |  Yep!  | maxp 2x2p0_2                     | (?, 52, 52, 64)
 Init  |  Yep!  | conv 3x3p1_1  +bnorm  leaky      | (?, 52, 52, 128)
 Load  |  Yep!  | maxp 2x2p0_2                     | (?, 26, 26, 128)
 Init  |  Yep!  | conv 3x3p1_1  +bnorm  leaky      | (?, 26, 26, 256)
 Load  |  Yep!  | maxp 2x2p0_2                     | (?, 13, 13, 256)
 Init  |  Yep!  | conv 3x3p1_1  +bnorm  leaky      | (?, 13, 13, 512)
 Load  |  Yep!  | maxp 2x2p0_1                     | (?, 13, 13, 512)
 Init  |  Yep!  | conv 3x3p1_1  +bnorm  leaky      | (?, 13, 13, 1024)
 Init  |  Yep!  | conv 3x3p1_1  +bnorm  leaky      | (?, 13, 13, 1024)
 Init  |  Yep!  | conv 1x1p0_1    linear           | (?, 13, 13, 35)
-------+--------+----------------------------------+---------------
GPU mode with 1.0 usage
Loading from ./ckpt/tiny-yolo-voc-2c-1500
INFO:tensorflow:Restoring parameters from ./ckpt/tiny-yolo-voc-2c-1500
Finished in 4.770599842071533s

Sample Dent Image

In [22]:
dent_img = cv2.imread('new-image-data/images_chem4/akhand_b43_110.jpg', cv2.IMREAD_COLOR)
dent_img = cv2.cvtColor(dent_img, cv2.COLOR_BGR2RGB)
result = tfnet.return_predict(dent_img)
In [23]:
for i in range(len(result)): 
    tl = (result[i]['topleft']['x'], result[i]['topleft']['y'])
    br = (result[i]['bottomright']['x'], result[i]['bottomright']['y'])
    label = result[i]['label']

    dent_img = cv2.rectangle(dent_img, tl, br, (0,255,0), 7)

    dent_img = cv2.putText(dent_img, label, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,0), 2)

plt.imshow(dent_img)
plt.show()

Sample Scratch or Spot

In [42]:
scratch_img = cv2.imread('new-image-data/images_chem4/akhand_b43_100.jpg', cv2.IMREAD_COLOR)
scratch_img = cv2.cvtColor(scratch_img, cv2.COLOR_BGR2RGB)
result = tfnet.return_predict(scratch_img)

result
Out[42]:
[{'label': 'Scratch_or_spot',
  'confidence': 0.6935361,
  'topleft': {'x': 342, 'y': 208},
  'bottomright': {'x': 383, 'y': 267}}]
In [43]:
for i in range(len(result)): 
    tl = (result[i]['topleft']['x'], result[i]['topleft']['y'])
    br = (result[i]['bottomright']['x'], result[i]['bottomright']['y'])
    label = result[i]['label']

    scratch_img = cv2.rectangle(scratch_img, tl, br, (0,255,0), 7)

    scratch_img = cv2.putText(scratch_img, label, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,0), 2)
    
plt.imshow(scratch_img)
plt.show()

Sample output on 50 images

In [51]:
import os

def get_results():
    for img in os.listdir('./sample-images/'):
        img_path = os.path.join("./sample-images/", img)

        input_img = cv2.imread(img_path, cv2.IMREAD_COLOR)
        input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB)
        result = tfnet.return_predict(input_img)
        
        for i in range(len(result)): 
            tl = (result[i]['topleft']['x'], result[i]['topleft']['y'])
            br = (result[i]['bottomright']['x'], result[i]['bottomright']['y'])
            label = result[i]['label']

            input_img = cv2.rectangle(input_img, tl, br, (0,255,0), 7)

            input_img = cv2.putText(input_img, label, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,0), 2)

        plt.imshow(input_img)
        plt.show()
In [52]:
get_results()